package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.vo.ContactVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MessageManager {

    @DubboReference
    UserInfoService userInfoService;

    @DubboReference
    QuestionService questionService;

    @Autowired
    HuanXinTemplate huanXinTemplate;

    @DubboReference
    FriendService friendService;

    @DubboReference
    CommentService commentService;

    public void replyStrangerQuestion(Long recommendUserId, String reply) {
        //1.获取我的userInfo
        Long userId = UserHolder.get().getId();
        UserInfo userInfo = userInfoService.findByUserId(userId);

        //2.通过推荐用户id获取ta的陌生人问题
        Question question = questionService.findByUserId(recommendUserId);
        if (question == null) {
            question = new Question();
        }

        //3.封装要通过环信发送的信息(map)
        Map map = new HashMap();
        map.put("userId",userId);
        map.put("huanXinId","HX"+userId);
        map.put("nickname",userInfo.getNickname());
        map.put("strangerQuestion",question.getStrangerQuestion());
        map.put("reply",reply);

        //4.调用huanxinTemplate发送信息给推荐用户
        huanXinTemplate.sendMsg("HX"+recommendUserId, JSON.toJSONString(map));
    }

    public void addContact(Long friendId) {
        //0.获取登陆者id
        Long userId = UserHolder.get().getId();

        //1.远程调用service完成好友添加操作
        friendService.addContact(userId,friendId);

        //2.在环信上建立好友关系
        huanXinTemplate.addContacts("HX"+userId,"HX"+friendId);
    }

    //好友列表
    public ResponseEntity findContactsByPage(Integer pageNum, Integer pageSize) {
        //1.获取登陆者id
        Long userId = UserHolder.get().getId();

        //2.远程调用service查询好友列表
        PageBeanVo pageBeanVo = friendService.findContactsByPage(userId, pageNum, pageSize);

        //3.获取好友列表中每个好友id,查询好友的userInfo,将userInfo封装ContactVo,注意(userId是环信id),将vo放入新集合voList中
        List<ContactVo> voList = new ArrayList<>();

        List<Friend> items = (List<Friend>) pageBeanVo.getItems();

        if (CollUtil.isNotEmpty(items)) {
            items.forEach(e->{
                Long friendId = e.getFriendId();
                UserInfo userInfo = userInfoService.findByUserId(friendId);

                ContactVo vo = new ContactVo();
                vo.setUserInfo(userInfo);
                vo.setUserId("HX"+friendId); //是环信id

                voList.add(vo);
            });
        }

        //4.将voList封装到分页对象
        pageBeanVo.setItems(voList);

        //5.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findCommentsByPage(Integer pageNum, Integer pageSize, Integer commentType) {
        //1.获取当前登陆者id
        //2.远程调用service查询pageBeanVo
        PageBeanVo pageBeanVo = commentService.findByPage(UserHolder.get().getId(), commentType, pageNum, pageSize);

        //3.获取pageBeanvo中集合(comment),只有操作者id,遍历集合,获取每个操作者userInfo,将userInfo和comment封装成页面上需要的数据,将每个vo对象放入voList集合(先创建处理)中
        List<UserCommentVo> voList = new ArrayList<>();

        List<Comment> items = (List<Comment>) pageBeanVo.getItems();

        if (CollUtil.isNotEmpty(items)) {
            items.forEach(e->{
                //获取每个操作者userInfo
                UserInfo userInfo = userInfoService.findByUserId(e.getUserId());

                //将userInfo和comment封装成页面上需要的数据对象
                UserCommentVo vo = new UserCommentVo();
                vo.setId(userInfo.getId().toString());//可以写用户id 也可以写评论
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(e.getCreated())));//创建时间

                voList.add(vo);
            });
        }

        //4.将voList设置给pageBeanVo
        pageBeanVo.setItems(voList);

        //5.返回pageBeanvo
        return ResponseEntity.ok(pageBeanVo);
    }
}
